/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Feb 13, 2013  4:11:54 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : MmLivcontractorDeductServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmLivcontractorDeduct;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class MmLivcontractorDeductServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(MmLivcontractorDeductServices.class);

	/**
	 * @param em
	 */
	public MmLivcontractorDeductServices(EntityManager em) {
		super(em);
	}

	public MmLivcontractorDeduct findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "MmLivcontractorDeduct.findById"));				
	}	
	
	public List<MmLivcontractorDeduct> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmLivcontractorDeduct.findAllIsSync", mapWhereValue));				
	}
	
	public List<MmLivcontractorDeduct> findByIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		mapWhereValue.put("isactive", "Y");

		return super.ConvertToYoureObject(super.find("MmLivcontractorDeduct.findByIsSync", mapWhereValue) );
	}	
	
	public List<MmLivcontractorDeduct> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "MmLivcontractorDeduct.findByInstanceNo"));				
	}
	
	public MmLivcontractorDeduct UpdatedIsSyncYes(MmLivcontractorDeduct value) throws Exception
	{
		if(value != null)
		{		
			value.setIssync("Y");
			return (MmLivcontractorDeduct)super.updated(value);
		}else
			return null;
	}
	
	public MmLivcontractorDeduct Updated(MmLivcontractorDeduct value) throws Exception
	{
		if(value != null)
		{		
			return (MmLivcontractorDeduct)super.updated(value);
		}else
			return null;
	}
	
	public MmLivcontractorDeduct InsertedOrUpdated(MmLivcontractorDeduct vlaue) throws Exception {
		MmLivcontractorDeduct obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("MM_LIVCONTRACTOR_DEDUCT");					
				obj.setPromisStagingId(id);
				obj.setCreatedBy(this.getUser());
				obj.setUpdatedBy(this.getUser());				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}

	public List<MmLivcontractorDeduct> Inserted(List<MmLivcontractorDeduct> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmLivcontractorDeduct obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("MM_LIVCONTRACTOR_DEDUCT");					
					obj.setPromisStagingId(id);
					obj.setCreatedBy(this.getUser());
					obj.setUpdatedBy(this.getUser());				
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}	
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<MmLivcontractorDeduct> Updated(List<MmLivcontractorDeduct> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmLivcontractorDeduct obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			super.updated(common_lst);
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
}
